package com.online_exam.controller.student.ExamPaper;

import com.github.pagehelper.PageInfo;
import com.online_exam.base.RestResponse;
import com.online_exam.base.UserType;
import com.online_exam.domain.ExamPaperAnswer;
import com.online_exam.domain.Subject;
import com.online_exam.domain.User;
import com.online_exam.service.ExamAnswerService;
import com.online_exam.service.ExamService;
import com.online_exam.service.SubjectService;
import com.online_exam.service.UserService;
import com.online_exam.viewmodel.admin.ExamPaper.ExamPaperEditRequestVM;
import com.online_exam.viewmodel.student.exampaper.ExamPaperAnswerPageResponseVM;
import com.online_exam.viewmodel.student.exampaper.ExamPaperAnswerPageVM;
import com.online_exam.viewmodel.student.exampaper.ExamPaperReadVM;
import com.online_exam.viewmodel.student.exampaper.ExamPaperSubmitVM;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController("StudentExamPaperAnswerController")
@RequestMapping(value = "/api/student/exampaper/answer")
@AllArgsConstructor
public class ExamPaperAnswerController{

    @Autowired
    UserService userService;
    @Autowired
    ExamAnswerService examAnswerService;
    @Autowired
    ModelMapper modelMapper;
    @Autowired
    SubjectService subjectService;
    @Autowired
    ExamService examService;


    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    //参数：科目与用户
    public RestResponse<PageInfo<ExamPaperAnswerPageResponseVM>> pageList(@RequestBody @Valid ExamPaperAnswerPageVM model,HttpServletRequest request) {
        model.setCreateUser(userService.getCurrent(UserType.Student,request.getCookies()).getId());
        //取答卷实体 -无题目，只有简单信息
        PageInfo<ExamPaperAnswer> pageInfo = examAnswerService.studentPage(model);
        //转答卷VM
        PageInfo<ExamPaperAnswerPageResponseVM> pageInfo2 = new PageInfo<>();
        List<ExamPaperAnswerPageResponseVM> list = pageInfo.getList().stream().map(e -> {
            ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
            Subject subject = subjectService.findById(vm.getSubjectId());
            vm.setId(e.getId());
            vm.setDoTime(e.getDoTime().toString());
            vm.setSystemScore(e.getSystemScore().toString());
            vm.setUserScore(e.getUserScore().toString());
            vm.setPaperScore(e.getPaperScore().toString());
            vm.setSubjectName(subject.getName());
            vm.setCreateTime(e.getCreateTime().toString());
            return vm;
        }).collect(Collectors.toList());
        pageInfo2.setList(list);
        return RestResponse.ok(pageInfo2);
    }

    @RequestMapping(value = "/answerSubmit", method = RequestMethod.POST)
    public RestResponse answerSubmit(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM,HttpServletRequest request) {
        User user = userService.getCurrent(UserType.Student, request.getCookies());
        //生成并插入答卷 -该方法会随便插入答题
        examAnswerService.examPaperAnswerFromVM(examPaperSubmitVM, user);
        return RestResponse.ok();
    }


    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public RestResponse edit(@RequestBody ExamPaperSubmitVM vm, HttpServletRequest request){
        System.out.println(vm);
        User user = userService.getCurrent(UserType.Admin, request.getCookies());

        return RestResponse.ok();
    }

    @RequestMapping(value = "/read/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperReadVM> read(@PathVariable Integer id) {
        //根据ID查答卷实体 -无题目，只有简单信息
        ExamPaperAnswer examPaperAnswer = examAnswerService.selectById(id);
        //创建答卷VM -包含原卷VM和答卷VM
        ExamPaperReadVM vm = new ExamPaperReadVM();
        //根据原卷id找原卷,并转为VM -存放题目等信息
        ExamPaperEditRequestVM paper = examService.examPaperToVM(examPaperAnswer.getExamPaperId());
        //根据答卷id找答卷，并转为VM -存放答卷信息和答题
        ExamPaperSubmitVM answer = examAnswerService.examPaperAnswerToVM(examPaperAnswer.getId());
        vm.setPaper(paper);
        vm.setAnswer(answer);
        return RestResponse.ok(vm);
    }


}
